(function(global, document, _fns, $, console) {
  'use strict';

  /*
  
      @name 自动嵌套命名空间
  
      @desc
  
      @author
  
      @access protected
  
      @function _extend
  
      @param {Object} ns 顶级命名空间
  
      @param {string} [nsString = tlns.ns1.ns2.ns3] 基于顶级命名空间要嵌套的命名空间
  
      @returns {Object} 返回嵌套好的对象。
  
      @example
          // " tlns.sample = $.noop; " === " tlns.ns1.ns2.ns3.sample = $.noop; "
          var tlns = _extend(tlns, "tlns.ns1.ns2.ns3");
  
      @todo 将这个函数放入公共函数库中。
   */
  var _extend, exc;
  _extend = function(ns, nsString) {
    var i, parent, parts, pl;
    parts = nsString.split(".");
    parent = ns;
    pl = parts.length;
    i = 0;
    while (i < pl) {
      if (typeof parent[parts[i]] === "undefined") {
        parent[parts[i]] = {};
      }
      parent = parent[parts[i]];
      i++;
    }
    return parent;
  };
  exc = _extend(_fns, "_fns.excute");

  /*
      @desc
  
      @author
  
      @access protected
  
      @function exc._fast_click
   */
  exc.sidebar = function() {
    global.lsBar();
  };

	  /*
	  @desc
	
	  @author
	
	  @access protected
	
	  @function exc._cancelOrder
	*/
	exc._cancelOrder = function() {
	var asd, btnCancelOrder, cbMdl, fnCancelOrder, mdl;
	btnCancelOrder = $('.cancel-order');
	mdl = $('#mdlCancelOrder');
	asd = mdl.html();
	mdl.remove();
	
	fnCancelOrder = function(e) {
		
	  var btnSubmit, fnCloseModal, frm, cancelOrderId, resonCode;
	  
	  e.stopPropagation();
	  e.preventDefault();
	  
	  var that = $(this);
	  
	  cancelOrderId = that.data('orderid');
	  
	  
	  ECar.easyDialog.open({
	    container: {
	      header: '取消定单？',
	      content: asd
	    }
	  });
	  
	  frm = $('#frmResnCancelOrder');
	  btnSubmit = frm.find('.btn-submit');
	  
	  var closestTd = that.closest('td');
	  
	  var prevTd = closestTd.prev('td');
	  
	  fnCloseModal = function(e) {
		  
		e.stopPropagation();
		e.preventDefault();
		
		resonCode = frm.find('input[type="radio"]:checked').val();
		resonCode = encodeURI(encodeURI(resonCode));
		console.log(resonCode);
		
	    if (frm.find('input[type="radio"]:checked').length > 0) {
	      var st = function () {
	         var url = "/member/toCancleOrder.htm?orderId="+cancelOrderId+"&resron="+resonCode;
	    	 var ajaxCancleOrder = $.post(url);
	    	 ajaxCancleOrder.done(function(data, textStatus, jqXHR) {
	    		 console.log(data);
	    		 if(data=='true'){
	    			 ECar.easyDialog.open({
	    				 container : {
	    					 header : '提示',
	    					 content : '<div style="text-align:center">定单取消成功</div>'
	    				 },
		    			 callback: location.reload(true) 
	    			 });
	    		 }else{
	    			 ECar.easyDialog.open({
	    				 container : {
	    					 header : '提示',
	    					 content : '<div style="text-align:center">定单取消失败</div>'
	    				 }
	    			 }); 
	    		 }
	         });
	    	 ajaxCancleOrder.fail(function(jqXHR, textStatus, errorThrown) {
	       	  ECar.easyDialog.open({
	   				container : {
	   					header : '提示',
	   					content : '<div style="text-align:center">定单取消失败</div>'
	   				}
	   			});
	         });
	      };
	      
	      st();
	      
	      ECar.easyDialog.close();
	      
	//      frm.submit();
	    }
	    
	  };
	  
	  btnSubmit.on('click', fnCloseModal);
	};
	btnCancelOrder.on('click', fnCancelOrder);
	};
	
	var btnChkOrder = $('.action').find('.check-order');
	//检查定单是否支付
	var checkOrderPayStatus = function (orderId, payUrl) {
		$.get("/member/orderStatus/"+orderId+".htm",
		function(data) {
			if('true'==data){
				console.log(orderId, payUrl);
				window.location.assign(payUrl);
			}else{
			  ECar.easyDialog.open({
				container : {
					header : '提示',
					content : '<div style="text-align:center">无效定单!</div>'
				}
			});
			}
		});
		return true;
	}
	btnChkOrder.on('click', function (e) {
	  e.stopPropagation();
	  e.preventDefault();
	  var that;
	  that = $(this);
	  checkOrderPayStatus(that.data('orderid'), that.data('payurl'));
	});
  
  /*
      @name 初始化
  
      @desc 所有在 " _fns.excute " 命名空间下定义的函数将被自动运行。
  
      @author
  
      @access protected
  
      @function _fns.init
  
      @param {Object} opts 存放定义好的可任意修改（配置）的行为选项。
   */
  _fns.init = function(opts) {
    var _it;
    for (_it in exc) {
      if (typeof exc[_it] === "function") {
        exc[_it]();
      }
    }
  };
  $(_fns.init);
})(window, document, {}, jQuery, (typeof console !== 'undefined' ? console : void 0));
